/*******************************************************************************
 * Copyright (c) 2010, 2030 www.itlaobing.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.itlaobing.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.itlaobing.model.OrderModel;
import cn.itlaobing.model.OrderStatus;
import cn.itlaobing.model.UserModel;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


/**
 * ClassName: OrderDao <br/> 
 * Function: 订单dao层 <br/> 
 * date: 2017年11月1日 下午2:35:12 <br/> 
 * 
 * @author wd
 * @version  
 * @since JDK 1.8 
 */
public class OrderDao extends BaseDao {

	private static final String fields="id,"+
			"orderid as orderId,"+
			"buytime as buyTime,"+
			"userid as userId,"+
			"totalmoney as totalMoney,"+
			"orderstatus as status,"+
			"orderfrom as sources,"+
			"receivername as receiverName,"+
			"receiverphone as receiverPhone,"+
			"address";


	public List<OrderModel> show(int userid)throws SQLException{
		QueryRunner qr=new QueryRunner(getDataSource());
		String sql="select * from orders where userid=?";
		BeanListHandler<OrderModel> beanListHandler=new BeanListHandler<OrderModel>(OrderModel.class);
		List<OrderModel> list=qr.query(sql,beanListHandler,userid);
		return list;
	}

	public OrderModel payment(String orderid) throws SQLException {
		QueryRunner qr=new QueryRunner(this.getDataSource());
		String sql="select"+fields+ "from orders where orderid=?";
		BeanHandler<OrderModel> orderModelBeanHandler= new BeanHandler<OrderModel>(OrderModel.class);
		OrderModel orderModel=qr.query(sql,orderModelBeanHandler,orderid);
		if(orderModel==null){
			return null;
		}
		if(orderModel.getStatus()== OrderStatus.NO_PAYMENT){
			sql="update orders set orderstatus=? where id=?";
			qr.update(sql,OrderStatus.ALLOCATE.name(),orderModel.getId());
			orderModel.setStatus(OrderStatus.ALLOCATE);
			System.out.println(OrderStatus.ALLOCATE.name());
			return orderModel;
		}
		return null;

	}

	
}
